//
//  TuiJianTableViewCell.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/22.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "TuiJianTableViewCell.h"


@interface TuiJianTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) BaseView *viewBackGround;  /**< 背景 */
@property (nonatomic, retain) TuiJianLayout *TuiJianlayout;
@property (nonatomic, retain) BaseCollectionView *collectionView;

@property (nonatomic, retain) BaseLabel *labelHeader;
@property (nonatomic, retain) BaseLabel *labelLine;
@end

@implementation TuiJianTableViewCell


#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    [_viewBackGround release];
    [_TuiJianlayout release];
    [_collectionView release];
    [_labelHeader release];
    [_labelLine release];
    [_arrTuiJian release];
    [super dealloc];
}



#pragma mark - ------------ tableview初始化 ---------------
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        /** 夜间 */
        self.contentView.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00],[UIColor lightGrayColor]);
        [self createCollectionView];
 

    }
    return self;
    
}

#pragma mark - ------------ 创建collectionView ---------------
- (void)createCollectionView
{
    
    //创建背景白色view
    self.viewBackGround = [[BaseView alloc]init];
    [self.contentView addSubview:_viewBackGround];

    
    self.TuiJianlayout = [[TuiJianLayout alloc]init];
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:self.viewBackGround.bounds collectionViewLayout:_TuiJianlayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    [self.viewBackGround addSubview:_collectionView];
    
    //注册cell
    [_collectionView registerClass:[NewCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [_TuiJianlayout release];
    [_collectionView release];
    [_viewBackGround release];
    
}

#pragma mark - ------------ 必须写的 赋大小 ---------------
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _viewBackGround.translatesAutoresizingMaskIntoConstraints = NO;
    
    [_viewBackGround mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.right.bottom.mas_equalTo(self.contentView).offset(0);

        
    }];
    
     [self layoutIfNeeded];
    _collectionView.frame = self.viewBackGround.bounds;

    
    
}

#pragma mark - ------------ cell赋值 ---------------
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
    EnjoyModel *m = _arrTuiJian[indexPath.row];
    cell.model = m;
    return cell;
    
}

#pragma mark - ------------ 返回4个item ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _arrTuiJian.count;
    
}

#pragma mark - ------------ item 点击 代理点击 判断点击哪个item法 ---------------

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.delegate respondsToSelector:@selector(TuiJianSeclect:)]) {
        
        [self.delegate TuiJianSeclect:indexPath];
        
    }
    
}



#pragma mark - ------------ 头部cell ---------------

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *header = [collectionView  dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        
        self.labelHeader = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 10, header.bounds.size.width, 50)];
        _labelHeader.text = @"推薦";
        _labelHeader.font = [UIFont systemFontOfSize:20];
        _labelHeader.textAlignment = NSTextAlignmentCenter;
        
        
        self.labelLine = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 35, header.bounds.size.width, 25)];
        _labelLine.text = @"______";
        _labelLine.textColor = [UIColor grayColor];
        _labelLine.textAlignment = NSTextAlignmentCenter;
        
        
        
        
        [header addSubview:_labelHeader];
        [header addSubview:_labelLine];
        
        [_labelHeader release];
        [_labelLine release];
        
        
        
        return header;
    }
    return nil;
    
    
}


#pragma mark - ------------ 重写 arr赋值 ---------------

- (void)setArrTuiJian:(NSArray *)arrTuiJian
{

    if (_arrTuiJian != arrTuiJian) {
        
        [_arrTuiJian release];
        _arrTuiJian = [arrTuiJian retain];
        
    }
    [self.collectionView reloadData];

}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
